package com.cheyunkeji.er.adapter.fastevaluate;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.adapter.MyBaseAdapter;
import com.cheyunkeji.er.bean.fast_evaluate.FastEvaluateRecord;
import com.cheyunkeji.er.utils.TimeUtils;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/** Created by Michael on 2017/3/31 14:19. */
public class EvaluateRecordsAdapter extends MyBaseAdapter {

  public static final int EVALUATE_STATE_WAIT_PUSH = 1;
  public static final int EVALUATE_STATE_PUSHED = 2;
  public static final int EVALUATE_STATE_ON_PROCESS = 3;
  public static final int EVALUATE_STATE_UPLOADED = 4;

  public EvaluateRecordsAdapter(ArrayList<FastEvaluateRecord> dataList, Context context) {
    super(dataList, context);
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder = null;
    if (convertView == null) {
      convertView =
          LayoutInflater.from(context).inflate(R.layout.er_fast_evaluate_records_item, parent, false);
      holder = new ViewHolder(convertView);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    FastEvaluateRecord bean = (FastEvaluateRecord) dataList.get(position);
    holder.tvName.setText(bean.getOwner_realname());
    holder.tvContactNumber.setText(bean.getOwner_phone());
    holder.tvPalateNumber.setText(bean.getPlate());
    holder.tvCarModel.setText( bean.getSpec_name());

    holder.tvEvaluateDate.setText(
        String.format(
            context.getResources().getString(R.string.evaluate_date),
            TimeUtils.millis2StringWithoutHMS(
                Long.valueOf(String.valueOf(bean.getDateline()) + "000"))));
    setCurrentState(holder.tvEvaluateState, Integer.valueOf(bean.getState()));

    if (Integer.parseInt(bean.getPush_type()) == 3) {
      holder.tvSellState.setText("已同步");
      holder.tvSellState.setTextColor(context.getResources().getColor(R.color.blue_2));
    } else {
      holder.tvSellState.setText("未同步");
      holder.tvSellState.setTextColor(context.getResources().getColor(R.color.wait_push));
    }
    return convertView;
  }

  // 设置评估记录当前状态
  private void setCurrentState(TextView tvEvaluateState, int evaluateState) {

    switch (evaluateState) {
      case EVALUATE_STATE_WAIT_PUSH:
        tvEvaluateState.setText(R.string.wait_push);
        tvEvaluateState.setTextColor(context.getResources().getColor(R.color.wait_push));
        break;
      case EVALUATE_STATE_PUSHED:
        tvEvaluateState.setText(R.string.pushed);
        tvEvaluateState.setTextColor(context.getResources().getColor(R.color.pushed));
        break;
      case EVALUATE_STATE_ON_PROCESS:
        tvEvaluateState.setText(R.string.evaluate_on_process);
        tvEvaluateState.setTextColor(context.getResources().getColor(R.color.blue_2));
        break;
      case EVALUATE_STATE_UPLOADED:
        tvEvaluateState.setText(R.string.uploaded);
        tvEvaluateState.setTextColor(context.getResources().getColor(R.color.uploaded));
        break;

      default:
        break;
    }
  }

  static class ViewHolder {
    @BindView(R.id.tv_name)
    TextView tvName;

    @BindView(R.id.tv_contact_number)
    TextView tvContactNumber;

    @BindView(R.id.tv_palate_number)
    TextView tvPalateNumber;

    @BindView(R.id.tv_car_model)
    TextView tvCarModel;

    @BindView(R.id.tv_evaluate_date)
    TextView tvEvaluateDate;

    @BindView(R.id.tv_evaluate_state)
    TextView tvEvaluateState;

    @BindView(R.id.tv_sell_state)
    TextView tvSellState;

    ViewHolder(View view) {
      ButterKnife.bind(this, view);
    }
  }
}
